import { message } from 'antd';
import {
  queryAccountList,
  queryAccount,
  setAccount,
  updateAccount,
  getOne,
  removeAccount,
  queryTmpList,
  setSchedule,
  queryTask,
  deleteTask,
} from '../services/wx';

export default {
  namespace: 'template_message',

  state: {
    accountList: [],
    tmpList: false,
    schedules: [],
    account: null,
  },

  effects: {
    // 查询公众号列表
    *fetchAccountList(_, { call, put }) {
      const res = yield call(queryAccountList);
      yield put({
        type: 'saveAccountList',
        payload: res,
      });
    },
    // 模糊查询公众号列表
    *fetchAccount({ payload }, { call, put }) {
      const res = yield call(queryAccount, payload);
      yield put({
        type: 'saveAccountList',
        payload: res,
      });
    },
    // 添加公众号
    *addAccount({ payload }, { call, put }) {
      const res = yield call(setAccount, payload);
      if (res === 'ok') {
        message.success('添加成功');
      } else if (res == 'exists') {
        message.error('账号已存在!', 8);
      } else {
        message.error('添加失败,请检查数据正确后重新添加!', 8);
      }
      yield put({
        type: 'fetchAccountList',
      });
    },
    // 获取某个公众号
    *getAccount({ payload }, { call, put }) {
      const res = yield call(getOne, payload);
      yield put({
        type: 'saveOne',
        payload: res,
      });
    },
    // 编辑修改公众号
    *editAccount({ payload }, { call, put }) {
      const res = yield call(updateAccount, payload);
      if (res === 'ok') {
        message.success('修改成功');
      } else {
        message.error('修改失败,请重新修改');
      }
      yield put({
        type: 'fetchAccountList',
      });
    },
    // 删除公众号
    *deleteAccount({ payload }, { call, put }) {
      const res = yield call(removeAccount, payload);
      if (res === 'ok') {
        message.success('删除成功');
      } else {
        message.error('删除失败,请稍后重试!');
      }
      yield put({
        type: 'fetchAccountList',
      });
    },
    // 查询公众号下的可配置的模板消息列表
    *fetchTmpList({ payload }, { call, put }) {
      const res = yield call(queryTmpList, payload);
      yield put({
        type: 'saveTmpList',
        payload: res,
      });
    },
    // 创建发送任务
    *createSchedule({ payload }, { call }) {
      const res = yield call(setSchedule, payload);
      if (res === 'ok') {
        message.success('操作成功');
      }
    },
    // 查询定时任务
    *fetchSchedules(_, { call, put }) {
      const res = yield call(queryTask);
      yield put({
        type: 'saveSchedules',
        payload: res,
      });
    },
    // 删除定时任务
    *deleteSchedule({ payload }, { call, put }) {
      const res = yield call(deleteTask, payload);
      if (res === 'ok') {
        message.success('删除成功');
        yield put({
          type: 'fetchSchedules',
        });
      } else {
        message.error('删除失败');
      }
    },
  },

  reducers: {
    // 保存账号列表
    saveAccountList(state, { payload }) {
      return {
        ...state,
        accountList: payload,
      };
    },
    // 保存模板列表
    saveTmpList(state, { payload }) {
      return {
        ...state,
        tmpList: { ...payload },
      };
    },
    // 保存任务列表
    saveSchedules(state, { payload }) {
      return {
        ...state,
        schedules: payload,
      };
    },
    // 保存单个账号
    saveOne(state, { payload }) {
      return {
        ...state,
        account: payload,
      };
    },
  },
};
